package com.mingtu.jgspatrol.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.StringUtils;
import com.mingtu.jgspatrol.R;
import com.mingtu.infrared.bean.InfraredWarningBean;

import java.util.List;

public class EcWarningRecordAdapter extends RecyclerView.Adapter<EcWarningRecordAdapter.ActivityHolder> implements View.OnClickListener {
    private Context context;
    private List<InfraredWarningBean.PageBean.ListBean> mData;
    private OnItemClickListener mOnItemClickListener = null;


    public EcWarningRecordAdapter(List<InfraredWarningBean.PageBean.ListBean> data) {
        this.mData = data;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ec_warning_record, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {
        if (mData != null) {
            if (mData.size() > 0) {
                InfraredWarningBean.PageBean.ListBean listBean = mData.get(position);
                String deviceName = listBean.getDeviceName(); //设备名字
                String time = listBean.getCreateTime();
                String msgType = listBean.getMsgType();
                String msgContent = listBean.getMsgContent();

                if (!StringUtils.isEmpty(deviceName)) {
                    holder.tvName.setText("设备编号：" + deviceName);
                }

                if (!StringUtils.isEmpty(time)) {
                    holder.tvWarningTime.setText("报警时间：" + time);
                }

                if (!StringUtils.isEmpty(msgType)) {
                    holder.tvWarningType.setText("报警类型：" + msgType);
                }

                if (!StringUtils.isEmpty(msgContent)) {
                    holder.tvContent.setText("报警事件：" + msgContent);
                }

            }
        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public void upData(List<InfraredWarningBean.PageBean.ListBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void clearData() {
        mData.clear();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final View convertView;
        private TextView tvName;
        private TextView tvWarningType;
        private TextView tvWarningTime;
        private TextView tvContent;

        public ActivityHolder(View itemView) {
            super(itemView);
            convertView = itemView;
            tvName = (TextView) convertView.findViewById(R.id.tv_name);
            tvWarningType = (TextView) convertView.findViewById(R.id.tv_warning_type);
            tvWarningTime = (TextView) convertView.findViewById(R.id.tv_warning_time);
            tvContent = (TextView) convertView.findViewById(R.id.tv_content);
        }
    }


}
